﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Devices.Geolocation;
using System.Device.Location;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MS.TravelJourney.Final
{
    public partial class MapLocation : PhoneApplicationPage
    {
        MapLayer PushpinMapLayer;
        public MapLocation()
        {
            InitializeComponent();
            var positionCoord = new GeoCoordinate(26.86, 100.25);
            this.MyMap.Center = positionCoord;
            this.MyMap.ZoomLevel = 15;
        }

        async void InitMapsAsync()
        {
            Geolocator MyGeolocator = new Geolocator();
            MyGeolocator.DesiredAccuracyInMeters = 5;
            Geoposition myGeoPosition = null;
            try
            {
                myGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disabled in phone settings");
            }

            // Set the center on the map
            var positionCoord = new GeoCoordinate(myGeoPosition.Coordinate.Latitude, myGeoPosition.Coordinate.Longitude);
            this.MyMap.Center = positionCoord;
            this.MyMap.ZoomLevel = 15;
            DrawPushpin(positionCoord);
        }
        private void DrawPushpin(GeoCoordinate coord)
        {
            var aPushpin = CreatePushpinObject();

            //Creating a MapOverlay and adding the Pushpin to it.
            MapOverlay MyOverlay = new MapOverlay();
            MyOverlay.Content = aPushpin;
            MyOverlay.GeoCoordinate = coord;
            MyOverlay.PositionOrigin = new Point(0, 0.5);

            // Add the MapOverlay containing the pushpin to the MapLayer
            this.PushpinMapLayer.Add(MyOverlay);
        }
        private Grid CreatePushpinObject()
        {
            //Creating a Grid element.
            Grid MyGrid = new Grid();
            MyGrid.RowDefinitions.Add(new RowDefinition());
            MyGrid.RowDefinitions.Add(new RowDefinition());
            MyGrid.Background = new SolidColorBrush(Colors.Transparent);

            //Creating a Rectangle
            Rectangle MyRectangle = new Rectangle();
            MyRectangle.Fill = new SolidColorBrush(Colors.Black);
            MyRectangle.Height = 20;
            MyRectangle.Width = 20;
            MyRectangle.SetValue(Grid.RowProperty, 0);
            MyRectangle.SetValue(Grid.ColumnProperty, 0);

            //Adding the Rectangle to the Grid
            MyGrid.Children.Add(MyRectangle);

            //Creating a Polygon
            Polygon MyPolygon = new Polygon();
            MyPolygon.Points.Add(new Point(2, 0));
            MyPolygon.Points.Add(new Point(22, 0));
            MyPolygon.Points.Add(new Point(2, 40));
            MyPolygon.Stroke = new SolidColorBrush(Colors.Black);
            MyPolygon.Fill = new SolidColorBrush(Colors.Black);
            MyPolygon.SetValue(Grid.RowProperty, 1);
            MyPolygon.SetValue(Grid.ColumnProperty, 0);

            //Adding the Polygon to the Grid
            MyGrid.Children.Add(MyPolygon);
            return MyGrid;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            PushpinMapLayer = new MapLayer();
            MyMap.Layers.Add(PushpinMapLayer);
            base.OnNavigatedTo(e);
        }
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Road;
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Aerial;
        }

        private void ApplicationBarIconButton_Click3(object sender, EventArgs e)
        {
            InitMapsAsync();
        }
    }
}